<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AppointViewDatum;
use App\Models\Hospital;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class AppointViewDatumController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AppointViewDatum(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('type')->using([1=>'预约详情弹窗']);
            $grid->column('long_time');
//            $grid->column('able_id');
//            $grid->column('user_id');
            $grid->column('hospital_id')
                ->using(Hospital::query()->pluck('name','id')->toArray());
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->disableCreateButton();
            $grid->disableActions();
            $titles = [
                'type'=>'类型',
                'long_time'=>'时长',
                'hospital_id'=>'社区',
                'created_at'=>'时间',
                ];
            $grid->export($titles)->rows(function ($rows) {
                foreach ($rows as $index => &$row) {
                    //$hospital = Hospital::query()->where('id',$row['hospital_id'])->first();
                    $row['hospital_id'] ="北宫镇社区卫生服务中心";
                    $row['type'] ="预约详情弹窗";

                }
                return $rows;
            });
            //$grid->export($titles);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->between('created_at')->date();
                $filter->equal('hospital_id','社区医院')->select(Hospital::query()->pluck('name','id')->toArray());

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AppointViewDatum(), function (Show $show) {
            $show->field('id');
            $show->field('type');
            $show->field('long');
            $show->field('able_id');
            $show->field('user_id');
            $show->field('hospital_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AppointViewDatum(), function (Form $form) {
            $form->display('id');
            $form->text('type');
            $form->text('long');
            $form->text('able_id');
            $form->text('user_id');
            $form->text('hospital_id');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
